<?php
namespace app\admin\controller;

use app\common\model\AdminUserLogModel;
use app\common\model\FieldAccessModel;
use app\common\model\MenuFieldModel;
use app\common\model\MenuModel;
use app\common\model\RoleModel;

class Menu extends Common
{
    /**
     * 添加菜单
     * @Author 贺强
     * @date   2018-08-23
     * @param  MenuModel  $m MenuModel 实例
     */
    public function add(MenuModel $m)
    {
        if ($this->request->isAjax()) {
            $param = $this->request->post();
            if (empty($param['identity']) || empty($param['title'])) {
                return ['status' => 3, 'info' => '非法操作'];
            }
            $menu = $m->getModel(['is_delete' => 0, 'identity' => $param['identity']]);
            if (!empty($menu)) {
                return ['status' => 1, 'info' => '菜单标识重复，请重新填写'];
            }
            if (empty($param['sort'])) {
                $param['sort'] = 99;
            }
            $res = $m->add($param);
            if ($res) {
                return ['status' => 0, 'info' => '添加成功'];
            }
            return ['status' => 2, 'info' => '添加失败'];
        } else {
            $mods = $m->getList([['modular', '=', 0], ['is_delete', '=', 0]], ['id', 'title']);
            return $this->fetch('add', ['mods' => $mods]);
        }
    }

    /**
     * AJAX获取模块下的菜单
     * @author 贺强
     * @time   2019-05-27 14:56:13
     * @param  MenuModel $m MenuModel 实例
     */
    public function get_menu(MenuModel $m)
    {
        $mid  = $this->request->post('mid');
        $menu = [];
        if ($mid) {
            $menu = $m->getList([['modular', '=', $mid], ['pid', '=', 0], ['is_delete', '=', 0]], ['id', 'title']);
        }
        return ['status' => 0, 'menu' => $menu];
    }

    /**
     * 删除菜单
     * @Author 贺强
     * @date   2018-08-23
     * @param  MenuModel  $m MenuModel 实例
     */
    public function del(MenuModel $m)
    {
        $id  = $this->request->post('id');
        $res = $m->modifyField('is_delete', 1, [['id', '=', $id]]);
        if ($res) {
            $m->modifyField('is_delete', 1, [['pid', '=', $id]]);
            return ['status' => 0, 'info' => '删除成功'];
        }
        return ['status' => 4, 'info' => '删除失败'];
    }

    /**
     * 编辑菜单
     * @Author 贺强
     * @date   2018-08-23
     * @param  MenuModel  $m MenuModel 实例
     */
    public function edit(MenuModel $m)
    {
        if ($this->request->isAjax()) {
            $param = $this->request->post();
            if (empty($param['id']) || empty($param['identity']) || empty($param['title'])) {
                return ['status' => 3, 'info' => '非法操作'];
            }
            $res = $m->modify($param, [['id', '=', $param['id']]]);
            if ($res !== false) {
                $m->modifyField('sort', $param['sort'], [['pid', '=', $param['id']]]);
                return ['status' => 0, 'info' => '修改成功'];
            } else {
                return ['status' => 4, 'info' => '修改失败'];
            }
        } else {
            $menu = null;
            $id   = $this->request->get('id');
            if (!empty($id) && is_numeric($id)) {
                $menu = $m->getModel([['id', '=', $id]]);
            }
            return $this->fetch('edit', ['menu' => $menu]);
        }
    }

    /**
     * 菜单列表
     * @Author 贺强
     * @date   2018-08-23
     */
    public function lists()
    {
        $r     = new RoleModel();
        $m     = new MenuModel();
        $roles = $r->getList();
        $where = ['is_delete' => 0];
        // 分页参数
        $page     = intval($this->request->get('page', 1));
        $pagesize = intval($this->request->get('pagesize', config('PAGESIZE')));
        $list     = $m->getList($where, true, "$page,$pagesize", 'modular,pid,sort');
        // var_dump($list);exit;
        $count = $m->getCount($where);
        $pages = ceil($count / $pagesize);
        return $this->fetch('list', ['list' => $list, 'roles' => $roles, 'pages' => $pages]);
    }

    /**
     * 修改排序
     * @author 贺强
     * @time   2019-06-26 14:23:35
     * @param  MenuModel $m MenuModel 实例
     */
    public function operatesort(MenuModel $m)
    {
        if (!$this->request->isAjax()) {
            $this->error('非法操作');
        }
        $param = $this->request->post();
        if (empty($param['id']) || empty($param['sort'])) {
            return ['status' => 1, 'info' => '参数缺失'];
        }
        $res = $m->modifyField('sort', $param['sort'], [['id', '=', $param['id']]]);
        if ($res !== false) {
            return ['status' => 0, 'info' => '修改成功'];
        }
        return ['status' => 4, 'info' => '修改失败'];
    }

    /**
     * 菜单权限管理
     * @Author 贺强
     * @date   2018-08-23
     */
    public function power()
    {
        $r = new RoleModel();
        if ($this->request->isAjax()) {
            $param    = $this->request->post();
            $menu_ids = '';
            if (!empty($param['menu_ids'])) {
                $menu_ids = implode(',', $param['menu_ids']);
            }
            $res = $r->modifyField('menus', $menu_ids, ['id' => $param['role_id']]);
            if ($res !== false) {
                return ['status' => 0, 'info' => '修改成功'];
            }
            return ['status' => 4, 'info' => '修改失败'];
        } else {
            $m       = new MenuModel();
            $roles   = $r->getList([['id', '<>', 1]]);
            $role_id = $this->request->get('role_id', 0);
            $has     = $r->getFieldValue('menus', ['id' => $role_id]);
            $list    = $m->getList([['is_delete', '=', 0]], true, null, 'modular,pid,sort');
            $arr     = [];
            foreach ($list as $item) {
                if ($item['pid'] === 0) {
                    $arr[] = $item;
                } else {
                    foreach ($arr as &$item2) {
                        if ($item['pid'] === $item2['id']) {
                            $item2['children'][] = $item;
                            break;
                        }
                    }
                }
            }
            $has = explode(',', $has);
            return $this->fetch('power', ['list' => $arr, 'roles' => $roles, 'has' => $has, 'role_id' => $role_id]);
        }
    }

    /**
     * 添加字段权限
     * @author 贺强
     * @time   2019-07-17 10:52:42
     * @param  FieldAccessModel $fa FieldAccessModel 实例
     */
    public function addfield(FieldAccessModel $fa)
    {
        if (!$this->request->isAjax()) {
            $r    = new RoleModel();
            $role = $r->getList([], ['id', 'role_name']);
            $m    = new MenuModel();
            $menu = $m->getList([['type', '=', 'list']], ['id', 'title']);
            return $this->fetch('addfield', ['role' => $role, 'menu' => $menu]);
        }
        $param = $this->request->post();
        if (empty($param['role_id'])) {
            return ['status' => 1, 'info' => '请选择角色'];
        }
        if (empty($param['menu_id'])) {
            return ['status' => 3, 'info' => '请选择菜单'];
        }
        if (empty($param['ziduan'])) {
            return ['status' => 5, 'info' => '请选择字段'];
        }
        $ziduan          = implode(',', $param['ziduan']);
        $param['ziduan'] = $ziduan;
        if (!empty($param['btn'])) {
            $btn          = implode(',', $param['btn']);
            $param['btn'] = $btn;
        }
        $res = $fa->add($param);
        if (!$res) {
            return ['status' => 4, 'info' => '添加失败'];
        }
        return ['status' => 0, 'info' => '添加成功'];
    }

    /**
     * 删除字段权限
     * @author 贺强
     * @time   2019-07-17 13:40:59
     * @param  FieldAccessModel $fa FieldAccessModel 实例
     */
    public function delfield(FieldAccessModel $fa)
    {
        if (!$this->request->isAjax()) {
            return ['status' => 1, 'info' => '非法操作'];
        }
        $id = $this->request->post('id');
        if (empty($id)) {
            return ['status' => 3, 'info' => '非法参数'];
        }
        $res = $fa->delById($id);
        if (!$res) {
            return ['status' => 4, 'info' => '删除失败'];
        }
        return ['status' => 0, 'info' => '删除成功'];
    }

    /**
     * 修改字段权限
     * @author 贺强
     * @time   2019-07-17 11:53:49
     * @param  FieldAccessModel $fa FieldAccessModel 实例
     */
    public function editfield(FieldAccessModel $fa)
    {
        if ($this->request->isAjax()) {
            $id    = $this->request->post('id');
            $param = $this->request->post();
            if (empty($id) || empty($param)) {
                return ['status' => 1, 'info' => '参数缺失'];
            }
            $param['ziduan'] = implode(',', $param['ziduan']);
            if (!empty($param['btn'])) {
                $param['btn'] = implode(',', $param['btn']);
            } else {
                $param['btn'] = '';
            }
            $res = $fa->modify($param, [['id', '=', $id]]);
            if ($res === false) {
                return ['status' => 4, 'info' => '修改失败'];
            }
            return ['status' => 0, 'info' => '修改成功'];
        } else {
            $id    = $this->request->get('id');
            $field = $fa->getModel([['id', '=', $id]]);
            // 修改字段数据类型
            $field['ziduan'] = explode(',', $field['ziduan']);
            $field['btn']    = explode(',', $field['btn']);
            // 该菜单字段
            $mf     = new MenuFieldModel();
            $fields = $mf->getList([['menu_id', '=', $field['menu_id']]], ['id', 'field_name', 'type']);
            return $this->fetch('editfield', ['field' => $field, 'fields' => $fields]);
        }
    }

    /**
     * 获取菜单字段
     * @author 贺强
     * @time   2019-07-17 11:14:16
     * @param  MenuFieldModel $mf MenuFieldModel 实例
     */
    public function getfields(MenuFieldModel $mf)
    {
        $param = $this->request->post();
        if (empty($param['menu_id'])) {
            return ['status' => 1, 'info' => '菜单ID不能为空'];
        }
        $list = $mf->getList([['menu_id', '=', $param['menu_id']]], ['id', 'field_name', 'type']);
        if (empty($list)) {
            return ['status' => 4, 'info' => '获取失败'];
        }
        return ['status' => 0, 'info' => '获取成功', 'data' => $list];
    }

    /**
     * 字段权限管理
     * @author 贺强
     * @time   2019-07-17 10:25:52
     * @param  FieldAccessModel $fa FieldAccessModel 实例
     */
    public function fields(FieldAccessModel $fa)
    {
        $where = [];
        $param = $this->request->get();
        if (!empty($param['menu_id'])) {
            $where[] = ['menu_id', '=', $param['menu_id']];
        }
        $page     = !empty($param['page']) ? $param['page'] : 1;
        $pagesize = config('PAGESIZE');
        $pages    = 0;
        if (!empty($param['role_id'])) {
            $where[] = ['role_id', '=', $param['role_id']];
        }
        $list = $fa->getList($where, true, "$page,$pagesize");
        if ($list) {
            $r    = new RoleModel();
            $role = $r->getList([], ['id', 'role_name']);
            $role = array_column($role, 'role_name', 'id');
            $m    = new MenuModel();
            $menu = $m->getList([['type', '=', 'list']], ['id', 'title']);
            $menu = array_column($menu, 'title', 'id');
            foreach ($list as &$item) {
                if (!empty($role[$item['role_id']])) {
                    $item['role_name'] = $role[$item['role_id']];
                } else {
                    $item['role_name'] = '';
                }
                if (!empty($menu[$item['menu_id']])) {
                    $item['title'] = $menu[$item['menu_id']];
                } else {
                    $item['title'] = '';
                }
            }
        }
        $count = $fa->getCount($where);
        $pages = ceil($count / $pagesize);
        return $this->fetch('fields', ['list' => $list, 'param' => $param, 'pages' => $pages]);
        $mf = new MenuFieldModel();
    }

    /**
     * 操作记录
     * @author 贺强
     * @time   2019-08-08 15:18:18
     * @param  AdminUserLogModel $aul AdminUserLogModel 实例
     */
    public function logs(AdminUserLogModel $aul)
    {
        $page     = $this->request->get('page', 1);
        $pagesize = config('PAGESIZE');
        $pages    = 0;
        $count    = $aul->getCount();
        $pages    = ceil($count / $pagesize);
        $list     = $aul->getList([], true, "$page,$pagesize", ['ctime' => 'desc']);
        foreach ($list as &$item) {
            if (!empty($item['login_time'])) {
                format_datetime($item['login_time']);
            }
            if (!empty($item['ctime'])) {
                format_datetime($item['ctime']);
            }
        }
        return $this->fetch('logs', ['list' => $list, 'pages' => $pages]);
    }

}
